﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace ThienNamElevator
{
    public partial class CSDL : Form
    {
        public CSDL()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        SqlCommandBuilder combui;
        SqlConnection con;
        DataTable dt;
        private string time1, time2;
        private bool isLe1=false, isLe2=false;
        private void CSDL_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nhapchitiet.Table_Total' table. You can move, or remove it, as needed.
            this.table_TotalTableAdapter.Fill(this.nhapchitiet.Table_Total);
            // TODO: This line of code loads data into the 'nhapchitiet.Table_Total' table. You can move, or remove it, as needed.
            this.table_TotalTableAdapter.Fill(this.nhapchitiet.Table_Total);
            // TODO: This line of code loads data into the 'nhapchitiet.Table_Total' table. You can move, or remove it, as needed.
            this.table_TotalTableAdapter.Fill(this.nhapchitiet.Table_Total);
            DefaultValues();
            ketnoiCSDL();
            loadData();
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            time1 = dateTimePicker1.Value.ToString("dd-MM-yyyy");
            time2 = dateTimePicker2.Value.ToString("dd-MM-yyyy");
            this.table_TotalTableAdapter.Fill(this.nhapchitiet.Table_Total);
        }
        private void DefaultValues()
        {
            cbbS1.SelectedIndex = 24;
            cbbS2.SelectedIndex = 0;
            cbbL1.SelectedIndex = 0;
            cbbL2.SelectedIndex = 0;
            //dateTimePicker1.Value = DateTime.Now.AddDays(1);
            dateTimePicker1.Value = DateTime.Now;
            dateTimePicker2.Value = DateTime.Now;
        }
        private void ketnoiCSDL()
        {
            con = new SqlConnection(@"Data Source=TRUNGDINH-PC;Initial Catalog=ThiennamNhatrang;Integrated Security=True");
            con.Open();
            da = new SqlDataAdapter("select * from Table_Total", con);
            ds = new DataSet();
            da.Fill(ds, "Table_Total");
            table_TotalDataGridView.DataSource = ds.Tables["Table_Total"];
        }
        private void loadData()
        {
            txtTen.DataBindings.Clear();
            txtTen.DataBindings.Add("Text", table_TotalDataGridView.DataSource, "ten");
            
            tonggioHeso1TextBox.DataBindings.Clear();
            tonggioHeso1TextBox.DataBindings.Add("Text", table_TotalDataGridView.DataSource, "tonggioHeso1");

            tonggioHeso2TextBox.DataBindings.Clear();
            tonggioHeso2TextBox.DataBindings.Add("Text", table_TotalDataGridView.DataSource, "tonggioHeso2");

            tonggioHeso3TextBox.DataBindings.Clear();
            tonggioHeso3TextBox.DataBindings.Add("Text", table_TotalDataGridView.DataSource, "tonggioHeso3");

            luongcobanTextBox.DataBindings.Clear();
            luongcobanTextBox.DataBindings.Add("Text", table_TotalDataGridView.DataSource, "luongcoban");

            phucapTextBox.DataBindings.Clear();
            phucapTextBox.DataBindings.Add("Text", table_TotalDataGridView.DataSource, "phucap");

            tongluongTextBox.DataBindings.Clear();
            tongluongTextBox.DataBindings.Add("Text", table_TotalDataGridView.DataSource, "tongluong");

        }
        private void saveData()
        {
            try
            {
                combui = new SqlCommandBuilder(da);
                da.Fill(ds, "Table_Total");
                da.Update(ds, "Table_Total");
                //MessageBox.Show("Cập nhật dữ liệu thành công"); 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể Update CSDL vào lúc này!", "Thông báo!", MessageBoxButtons.OK);
            }
            finally
            {
                ketnoiCSDL();
                loadData();
                con.Close();
            }
        }
        private void txtHs1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHs2.Focus();
            }
        }

        private void txtHs2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHs3.Focus();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string DayStart = dateTimePicker1.Value.ToString("dd-MM-yyyy");
            txtDayStart.Text = DayStart;
            try
            {
                if (time1.CompareTo(DayStart) > 0)
                    time1 = DayStart;
            }
            catch { }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            string DayLeft = dateTimePicker2.Value.ToString("dd-MM-yyyy");
            txtDayLeft.Text = DayLeft;
            try
            {
                if (time2.CompareTo(DayLeft) < 0)
                    time2 = DayLeft;
            }
            catch { }
        }

        private void CSDL_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("Bạn đã nhập dữ liệu thành công từ ngày " + time1 + " tới ngày " + time2, "Thông báo!", MessageBoxButtons.OK);
            saveData();
        }

        private void btnNgayle1_Click(object sender, EventArgs e)
        {
            isLe1 = !isLe1;
            if (isLe1 == true)
            {
                btnNgayle1.ForeColor = Color.Red;
                //MessageBox.Show(isLe1.ToString());
            }
            else
            {
                btnNgayle1.ForeColor = Color.Black;
                //MessageBox.Show(isLe1.ToString());
            }
            changed();

        }

        private void btnNgayle2_Click(object sender, EventArgs e)
        {
            isLe2 = !isLe2;
            if (isLe2 == true)
            {
                btnNgayle2.ForeColor = Color.Red;
                //MessageBox.Show(isLe1.ToString());
            }
            else
            {
                btnNgayle2.ForeColor = Color.Black;
                //MessageBox.Show(isLe1.ToString());
            }
            changed();
        }

        private void btnDataGirdView_Click(object sender, EventArgs e)
        {
            float heso1 = 0, heso2 = 0, heso3 = 0;
            //int currentIndex;
            heso1 = 0 + float.Parse(txtHs1.Text) + float.Parse(tonggioHeso1TextBox.Text);
            heso2 = 0 + float.Parse(txtHs2.Text) + float.Parse(tonggioHeso2TextBox.Text);
            heso3 = 0 + float.Parse(txtHs3.Text) + float.Parse(tonggioHeso3TextBox.Text);
            tonggioHeso1TextBox.Text = heso1.ToString();
            tonggioHeso2TextBox.Text = heso2.ToString();
            tonggioHeso3TextBox.Text = heso3.ToString();
            //this.table_TotalBindingSource.MoveNext();
            this.Validate();
            this.table_TotalBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.nhapchitiet);
            btnDataGirdView.Enabled = false;
            btnHoantac.Enabled = true;
            /*
            currentIndex = table_TotalDataGridView.SelectedRows[0].Index + 1;
            foreach (DataGridViewRow dgvr in table_TotalDataGridView.Rows)
            {
                if (dgvr.Selected == true)
                {
                    dgvr.Selected = false;
                }
            }
            
            table_TotalDataGridView.Rows[currentIndex].Selected = true;
            //table_TotalDataGridView.CurrentRow[0].Index = currentIndex;
             * */
        }
        
        private void table_TotalBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.table_TotalBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.nhapchitiet);

        }

        private void table_TotalDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                table_TotalDataGridView.CurrentRow.Selected = true;
                btnHoantac.Enabled = false;
                btnDataGirdView.Enabled = true;
            }
            catch (Exception ex)
            { }
        }

        private void table_TotalDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            table_TotalDataGridView.Rows[e.RowIndex].Cells["stt"].Value = e.RowIndex + 1;
        }
        private void changed()
        {
            float s = 0, s1 = 0, s2 = 0, l = 0, l1 = 0, l2 = 0;
            float heso1 = 0, heso2 = 0, heso3 = 0;
            string DayStart = dateTimePicker1.Value.ToString("dd-MM-yyyy");
            string DayLeft = dateTimePicker2.Value.ToString("dd-MM-yyyy");
            if (DayStart.CompareTo(DayLeft) > 0)
                MessageBox.Show("Ngày kết thúc phải lớn hơn hoặc bằng ngày bắt đầu", "Thông báo!", MessageBoxButtons.OK);
            else
            {
                try
                {
                    s1 = float.Parse(cbbS1.SelectedItem.ToString());
                    s2 = float.Parse(cbbS2.SelectedItem.ToString()) / 60;
                    s = 24 - (s1 + s2);

                    l1 = float.Parse(cbbL1.SelectedItem.ToString());
                    l2 = float.Parse(cbbL2.SelectedItem.ToString()) / 60;
                    l = l1 + l2;
                }
                catch (Exception ex)
                { }
                //gan cac he so
                if (isLe1 == true)
                {
                    heso3 += s;
                    if (isLe2 == true)
                        heso3 += l;
                    else
                    {
                        if (dateTimePicker2.Value.DayOfWeek == DayOfWeek.Sunday)
                            heso2 += l;
                        else
                            heso1 += l;
                    }
                }
                else
                {
                    if (dateTimePicker1.Value.DayOfWeek == DayOfWeek.Sunday)
                    {
                        heso2 += s;
                        if (isLe2 == true)
                            heso3 += l;
                        else
                            heso1 += l;
                    }
                    else
                    {
                        heso1 += s;
                        if (isLe2 == true)
                            heso3 += l;
                        else
                        {
                            if (dateTimePicker2.Value.DayOfWeek == DayOfWeek.Sunday)
                                heso2 += l;
                            else
                                heso1 += l;
                        }
                    }
                }
                //gan vao textbox
                txtHs1.Text = heso1.ToString();
                txtHs2.Text = heso2.ToString();
                txtHs3.Text = heso3.ToString();
            }
            
        }
        private void cbbL2_SelectedIndexChanged(object sender, EventArgs e)
        {
            changed();
        }

        private void cbbS1_SelectedIndexChanged(object sender, EventArgs e)
        {
            changed();
        }

        private void cbbS2_SelectedIndexChanged(object sender, EventArgs e)
        {
            changed();
        }

        private void cbbL1_SelectedIndexChanged(object sender, EventArgs e)
        {
            changed();
        }

        private void btnHoantac_Click(object sender, EventArgs e)
        {
            float heso1 = 0, heso2 = 0, heso3 = 0;
            //int currentIndex;
            heso1 = - float.Parse(txtHs1.Text) + float.Parse(tonggioHeso1TextBox.Text);
            heso2 = - float.Parse(txtHs2.Text) + float.Parse(tonggioHeso2TextBox.Text);
            heso3 = - float.Parse(txtHs3.Text) + float.Parse(tonggioHeso3TextBox.Text);
            tonggioHeso1TextBox.Text = heso1.ToString();
            tonggioHeso2TextBox.Text = heso2.ToString();
            tonggioHeso3TextBox.Text = heso3.ToString();
            //this.table_TotalBindingSource.MovePrevious();
            btnHoantac.Enabled = false;
            btnDataGirdView.Enabled = true;
            /*
            //table_TotalDataGridView ngày mai làm tiếp chỗ này. chọn hàng phía trước
            currentIndex = table_TotalDataGridView.SelectedRows[0].Index - 1;
            table_TotalDataGridView.Rows[currentIndex].Selected = true;
             * */

        }
    }
}
